package com.bilin.coin.tools;

import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.network.callback.CommonCallBack;
import com.bilin.coin.utils.DialogUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.net.SocketTimeoutException;
import java.util.Map;



public abstract class Geter {

    BaseAppCompatActivity appCompatActivity;
    private boolean isShowDialog = true;
    private boolean isShowErrorMsg = true;

    public Geter(BaseAppCompatActivity appCompatActivity) {
        this.appCompatActivity = appCompatActivity;
        get();
    }
    public Geter(BaseAppCompatActivity appCompatActivity, boolean isShowDialog) {
        this.appCompatActivity = appCompatActivity;
        this.isShowDialog = isShowDialog;
        get();
    }
    public Geter(BaseAppCompatActivity appCompatActivity, boolean isShowDialog, boolean isShowErrorMsg) {
        this.appCompatActivity = appCompatActivity;
        this.isShowDialog = isShowDialog;
        this.isShowErrorMsg = isShowErrorMsg;
        get();
    }
    private void get() {
        try {
            if(isShowDialog) {
                DialogUtils.showDialogLoading(appCompatActivity);
            }
            RequestUtils
                    .get()
                    .url(fillUrl())
                    .params(fillParams())
                    .build()
                    .compose(appCompatActivity.<String>bindUntilEvent(ActivityEvent.DESTROY))
                    .subscribe(new CommonCallBack() {

                        @Override
                        protected void onError(String errorMsg) {
                            if (isShowErrorMsg)  appCompatActivity.showToast(errorMsg);
                            DialogUtils.dismissDialogLoading();
                            disposeError(errorMsg);
                        }
                        @Override
                        protected void onSuccess(String dataJson) {
                            DialogUtils.dismissDialogLoading();
                            disposeSuccess(dataJson);
                        }

                        @Override
                        protected void onCallBackError(Throwable e) {
                            DialogUtils.dismissDialogLoading();
                            if (e instanceof SocketTimeoutException) {
                                appCompatActivity.showToast("网络开小差了");
                            } else {
                                appCompatActivity.showMsg(e);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 子类重写
     * @param
     */
    protected void disposeStopFresh() {

    }

    /**
     * 子类重写
     * @param dataJson
     */
    protected void disposeSuccess(String dataJson) {

    }

    /**
     * 子类重写
     * @param dataJson
     */
    protected void disposeError(String dataJson) {

    }

    protected abstract String fillUrl();

    protected abstract Map<String, String> fillParams();
}
